package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.util.oss.AliyunOSSOperators;
import com.qk.management.aop.anno.LogAnno;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-10-09 17:37
 */
@Slf4j
@RestController
@RequestMapping("/upload")
@SuppressWarnings("all")
public class FileUploadController {
    @Autowired
    private AliyunOSSOperators oss;
    @LogAnno
    @PostMapping
    public Result upload( MultipartFile image) {
        log.info("上传文件:{}",image.getOriginalFilename());
        try {
            Objects.requireNonNull( image,"文件不能为空");
            Objects.requireNonNull( image.getOriginalFilename(),"文件名不能为空");

            String originalFilename = image.getOriginalFilename();
            String fileName = System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));
            String uniqueFileName = UUID.randomUUID()
                    .toString().replace("-","")+fileName;
            String url = oss.upload(image.getBytes(),uniqueFileName);
            return Result.success(url);
        }catch (Exception e){
            log.error("上传文件失败:{}",e.getMessage());
            return Result.error("上传文件失败");
        }
    }
}
